import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/10 9:27
 * @description 动态规划
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.longestCommonSubsequence("abcde", "ace");
    }
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length(), n = text2.length();
        char[] t1 = text1.toCharArray(), t2 = text2.toCharArray();
        // dp[i][j]表示 text1[0..i] 和 text2[0...j]的最长公共子序列长度
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (t1[i-1] == t2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[m][n];
    }
}
